<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>重置密码</title>
    <link rel="stylesheet" href="/Sys/css/layui.css">
    <link rel="stylesheet" href="/Sys/css/sign.css">
    <script src="/Sys/js/layui.js"></script>
    <script src="/Sys/js/jquery-3.3.1.min.js"></script>
</head>
<body class="layui-unselect lau-sign-body" style="background-image: url('/Sys/images/mybg.jpg'); background-repeat: no-repeat;">
    <div class="layui-form layui-form-pane lau-sign-form">
        <h1 class="lau-sign-title">重置密码</h1>
        <div class="layui-form-item">
            <label class="layui-form-label">账号</label>
            <div class="layui-input-block">
                <input type="text" name="username" placeholder="请输入账号" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">手机号</label>
            <div class="layui-input-block">
                <input type="text" name="phone" placeholder="请输入手机号" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">新密码</label>
            <div class="layui-input-block">
                <input type="password" name="newPassword" placeholder="8-20位数字和字母组合" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">确认密码</label>
            <div class="layui-input-block">
                <input type="password" name="confirmPassword" placeholder="请再次输入密码" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <button type="button" class="layui-btn layui-btn-fluid" lay-submit lay-filter="resetPassword">重置密码</button>
        </div>
        <div class="layui-form-item">
            <a href="/Sys/login" class="layui-link">返回登录</a>
        </div>
    </div>

    <script>
        layui.use(['form', 'layer'], function() {
            var form = layui.form;
            var layer = layui.layer;
            
            form.on('submit(resetPassword)', function(data) {
                var field = data.field;
                
                // 验证密码格式
                if(!/^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,20}$/.test(field.newPassword)) {
                    layer.msg('密码必须是8-20位数字和字母的组合');
                    return false;
                }
                
                // 验证两次密码是否一致
                if(field.newPassword !== field.confirmPassword) {
                    layer.msg('两次输入的密码不一致');
                    return false;
                }
                
                // 发送重置密码请求
                $.ajax({
                    url: '/Sys/resetPassword',
                    type: 'POST',
                    contentType: 'application/json',
                    data: JSON.stringify({
                        username: field.username,
                        phone: field.phone,
                        newPassword: field.newPassword
                    }),
                    success: function(res) {
                        if(res === 'success') {
                            layer.msg('密码重置成功', {
                                icon: 1,
                                time: 2000
                            }, function() {
                                window.location.href = '/Sys/login';
                            });
                        } else if(res === 'invalid_user') {
                            layer.msg('账号不存在');
                        } else if(res === 'invalid_phone') {
                            layer.msg('手机号码不匹配');
                        } else {
                            layer.msg('密码重置失败');
                        }
                    }
                });
                
                return false;
            });
        });
    </script>
</body>
</html> 